package com.aaa.service.sys.impl;

import com.aaa.dao.sys.RoleDao;
import com.aaa.pojo.Menu;
import com.aaa.pojo.Role;
import com.aaa.service.sys.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public List<Role> findAll(String page, String limit) {
        int parsePage = Integer.parseInt(page);
        int parseLimit = Integer.parseInt(limit);
        int start = (parsePage - 1)*parseLimit;
        List<Role> all = roleDao.findAll(start,parseLimit);
        return all;
    }

    @Override
    public int findCount() {
        int count = roleDao.findCount();
        return count;
    }

    @Override
    public int delete(int roleId) {
        //int parseInt = Integer.parseInt(roleId);
        int delete = roleDao.delete(roleId);
        return delete;
    }

    @Override
    public void insertRoleMenu(int roleId, String[] menusId) {
        for (int i = 0; i < menusId.length; i++) {
            roleDao.insertRoleMenu(roleId, menusId[i]);
        }
    }

    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }

    @Override
    public List<Integer> findSelectMenu(int roleId) {
        return roleDao.findSelectMenu(roleId);
    }

    @Override
    public void updateRoleMenu(int roleId, String[] menusId) {
        roleDao.deleteRoleMenu(roleId);
        for (int i = 0; i < menusId.length; i++) {
            roleDao.insertRoleMenu(roleId, menusId[i]);
        }
    }

    @Override
    public Role insert(Role role) {
        this.roleDao.insert(role);
        return role;
    }

    @Override
    public Role update(Role role) {
        this.roleDao.update(role);
        return this.queryById(role.getRoleId());
    }

    @Override
    public Role queryById(Integer roleId) {
        return this.roleDao.queryById(roleId);
    }

    @Override
    public List<Role> queryAllRole() {
        return roleDao.queryAllRole();
    }
}
